<%layout("/common/_layout.html"){%>
<%#ajax userTable:{%>
<div class="am-cf am-padding" style="padding:1rem 1.6rem 1.6rem 1rem;margin:0px;">
    <!--  padding:1px 2px 3px 4px;上、右、下,和左   -->
    <div class="am-fl am-cf"> <strong class="am-text-primary am-text-lg">业务配置</strong>
        /
        <small>业务列表</small>
    </div>
</div>
<form class="am-form" id="list-form" action="${base}/tariff/list" method="post">
    <input type="hidden" id="keywords" name="keywords" value='${keywords!""}' />
    <table class="am-table am-table-bordered am-table-radius table-main">
        <tbody>
            <tr>
                <th class="am-text-middle">业务名称</th>
                <td>
                    <div class="am-u-sm-12">
                        <input id="tariff_name" name="tariff_name" type="text" class="am-form-field am-input-sm" value="${tariff_name!}"></div>
                </td>
                <th class="am-text-middle">业务代码</th>
                <td>
                    <div class="am-u-sm-12">
                        <input id="tariff_code" name="tariff_code" type="text" class="am-form-field am-input-sm" value="${tariff_code!}"></div>
                </td>
            </tr>
            <tr>
                <th class="am-text-middle">业务类型</th>
                <td>
                    <div class="am-u-sm-12">
                        <select data-am-selected="{btnWidth: '100%',maxHeight: 200,btnSize:'sm'}" name="tariff_type" id="tariff_type">
                            <option value="-1"<%if(tariff_type=="-1"){%>
                                selected
                                <%}%>>所有</option>
                            <option value="0" <%if(tariff_type=="0"){%>
                                selected
                                <%}%>>主资费</option>
                            <option value="1" <%if(tariff_type=="1"){%>
                                selected
                                <%}%>>附加资费</option>
                            <option value="2" <%if(tariff_type=="2"){%>
                                selected
                                <%}%>>营销活动</option>
                            <option value="3" <%if(tariff_type=="3"){%>
                                selected
                                <%}%>>营销活动（短信）</option>
                        </select>
                    </div>
                </td>
                <th class="am-text-middle">所属城市</th>
                <td>
                    <div class="am-u-sm-12">
                        <select select data-am-selected="{btnWidth: '100%',maxHeight: 200,btnSize:'sm'}" name="selected_city" id="selected_city">
                            <option value="-1" <%if(selected_city=="-1"){%>
                                selected
                                <%}%>>所有</option>
                            <%for(city in cityList){%>
                            <option value="${city.value}" <%if(selected_city==city.value){%>
                                selected
                                <%}%>>${city.key}</option>
                            <%}%></select>
                    </div>
                </td>
            </tr>
            <tr>
                <th class="am-text-middle">业务大类</th>
                <td>
                    <div class="am-u-sm-12">
                        <select select data-am-selected="{btnWidth: '100%',maxHeight: 200,btnSize:'sm',searchBox: 1}" name="tariff_class" id="tariff_class">
                            <option value="-1" <%if(tariff_class=="-1"){%>
                                selected
                                <%}%>>所有</option>
                            <%for(tariffClass in tariffClassList){%>
                            <option value="${tariffClass.value}" <%if(tariff_class==tariffClass.value){%>
                                selected
                                <%}%>>${tariffClass.key}</option>
                            <%}%></select>
                    </div>
                </td>
                <th class="am-text-middle">是否启用</th>
                <td>
                    <div class="am-u-sm-12">
                        <select data-am-selected="{btnWidth: '100%',maxHeight: 200,btnSize:'sm'}" name="is_enabled" id="is_enabled">
                            <option value="-1"<%if(is_enabled=="-1"){%>
                                selected
                                <%}%>>所有</option>
                            <option value="0" <%if(is_enabled=="0"){%>
                                selected
                                <%}%>>禁用</option>
                            <option value="1" <%if(is_enabled=="1"){%>
                                selected
                                <%}%>>启用</option>
                        </select>
                    </div>
                </td>
            </tr>
        </tbody>
    </table>
</form>

<div class="am-u-sm-12" >
    <div class="am-u-sm-7">
        <div class="am-btn-toolbar">
            <div class="am-btn-group am-btn-group-xs" >
                <%if(so.hasPermission("TARIFF_ADD")) {%>
                <button type="button" class="am-btn am-btn-default" onclick="location.href='${base}/tariff/add'">
                    <span class="am-icon-plus"></span>
                    新增
                </button>
                <%}%>
                <%if(so.hasPermission("TARIFF_DELETE")) {%>
                <button type="button" id="deleteButton" class="am-btn am-btn-default" onclick="deleteAll('${base}/tariff/delete')" disabled>
                    <span class="am-icon-trash-o"></span>
                    删除
                </button>
                <%}%>
                <%if(so.hasPermission("TARIFF_ENABLE")) {%>
                <button type="button" id="enableButton" class="am-btn am-btn-default" onclick="enableAll('${base}/tariff/enable')" disabled>
                    <span class="am-icon-toggle-on"></span>
                    启用
                </button>
                <%}%>
                <%if(so.hasPermission("TARIFF_DISABLE")) {%>
                <button type="button" id="disableButton" class="am-btn am-btn-default" onclick="disableAll('${base}/tariff/disable')" disabled>
                    <span class="am-icon-toggle-off"></span>
                    禁用
                </button>
                <%}%></div>
        </div>
    </div>
    <div class="am-u-sm-5">
        <div align='right'>
            <button type="button" class="am-btn am-btn-default am-btn-sm am-text-secondary" onClick="submit_btn('search');">搜索</button>
            <button type="button" class="am-btn am-btn-default am-btn-sm am-text-secondary" onClick="submit_btn('export');">导出</button>
        </div>
    </div>
</div>

<div class="am-g" >
    <div class="am-u-sm-12 am-scrollable-horizontal">
        <!--  padding:1px 2px 3px 4px;上、右、下,和左   -->
        <table class="am-table am-table-striped am-table-hover table-main">
            <thead>
                <tr class="am-text-nowrap">
                    <th class="table-check">
                        <input type="checkbox" id="allCheck"></th>
                    <th class="table-title">业务名称</th>
                    <th class="table-title">业务代码</th>
                    <th class="table-title">业务类型</th>
                    <th class="table-title">所属大类</th>
                    <th class="table-title">属地地市</th>
                    <th class="table-date">发布人</th>
                    <th class="table-date">发布人机构</th>
                    <th class="table-date">开始时间</th>
                    <th class="table-date">结束时间</th>
                    <th class="table-date">排序</th>
                    <th class="table-date">是否启用</th>
                    <th class="table-set am-text-center">操作</th>
                </tr>
            </thead>
            <tbody>
                <%for(list in pager.list!){%>
                <tr>
                    <td>
                        <input type="checkbox" name="ids" value="${list.id}"/>
                    </td>
                    <td>${list.name}</td>
                    <td>${list.code}</td>
                    <td>${list.type}</td>
                    <td>${list.tariff_class_name}</td>
                    <td><div title="${list.localized_cities!'统一授权'}" style="text-overflow:ellipsis;overflow:hidden;white-space:nowrap;width: 80px;">${list.localized_cities!'统一授权'}</div></td>
                    <td>${list.creatorName}</td>
                    <td>${list.creatorOrgName}</td>
                    <td>${list.start_date,dateFormat="yyyy-MM-dd"}</td>
                    <td>${list.end_date,dateFormat="yyyy-MM-dd"}</td>
                    <td>${list.sort}</td>
                    <td id='tariff_enabled'>
                        <%if(list.enabled == 1){%>
                        是
                        <%} else {%>
                        否
                        <%}%></td>
                    <td id='tariff_operated'>
                        <%if(so.hasPermission("TARIFF_EDIT")){%>
                        <div id ="edit-div" class="am-btn-toolbar <%if(list.enabled == 1) {%>
                            am-hide
                            <%}%>
                            ">
                            <div class="am-btn-group am-btn-group-xs">
                                <button type="button" class="am-btn am-btn-default am-btn-xs am-text-secondary" onclick="location.href='${base}/tariff/edit?id=${list.id}'">
                                    <span class="am-icon-pencil-square-o"></span>
                                    编辑
                                </button>
                            </div>
                        </div>
                        <%}%></td>
                </tr>
                <%}%></tbody>
        </table>

        <div class="am-cf">
            <!-- 分页 -->
            <%if (pager.list.~size >
            0){%>
            <div class="am-fr">
                <% include("/admin/common_pager.tpl"){}%></div>
            <%}else{%>
            <% include("/admin/waiting_hint.tpl"){}%>
            <div class="am-kai">
                <h3>没有找到任何记录!</h3>
            </div>
            <div style="height:200px;"></div>
            <%}%></div>
        <%}%></div>
</div>
<script type="text/javascript">
  var urlBase = "${base}";
  var url;

   var keywordsObj = {};
       var submit_btn = function(businessType){
           keywordsObj.tariff_name = $("#tariff_name").val();
           keywordsObj.tariff_code = $("#tariff_code").val();
           if($("#selected_city").val() != "-1"){
               keywordsObj.selected_city = $("#selected_city").val();
           }
           if( $("#tariff_type").val() != "-1"){
               keywordsObj.tariff_type = $("#tariff_type").val();
           }
           if($("#tariff_class").val() != "-1"){
               keywordsObj.tariff_class = $("#tariff_class").val();
           }
           if($("#is_enabled").val() != "-1"){
               keywordsObj.is_enabled = $("#is_enabled").val();
           }

           var keywords = "";
           if(!$.isEmptyObject(keywordsObj)){
               keywords = JSON.stringify(keywordsObj);
           }
           console.log("keywords = "+ keywords);
           $("#keywords").val(keywords);

           if(businessType == "search"){
              showWaitingHint("正在查询，请稍候...");
              window.location.href = "${base}/tariff/list?keywords="+keywords;
           }else if(businessType == "export"){
           	window.location.href = "${base}/tariff/export?keywords="+keywords;
           }
   }

  $(document).ready(function() {
    var $allCheck = $("#allCheck");// 全选复选框
    var $idsCheck = $('[name=ids]:checkbox');// ID复选框
    var $enableButton = $("#enableButton");// 删除按钮
    var $disableButton = $("#disableButton");// 查询按钮
    $allCheck.checkAll();
    // 全选
    $allCheck.click(function() {
        $allCheck.checkAll();
        var isChecked = $allCheck.is(":checked");
        if (isChecked == false) {
            $enableButton.prop("disabled", true);
            $disableButton.prop("disabled", true);
        } else {
            $enableButton.prop("disabled", false);
            $disableButton.prop("disabled", false);
        }
    });

    // 无复选框被选中时,删除按钮不可用
    $idsCheck.click(function() {
        var $idsChecked = $("[name='ids']:checked");
        if ($idsChecked.size() > 0) {
            $enableButton.prop("disabled", false);
            $disableButton.prop("disabled", false);
        } else {
            $enableButton.prop("disabled", true);
            $disableButton.prop("disabled", true);
        }
    });   
  });

  function enableAll(url){
    batchProcess($("#enableButton"), url, '启用','是');
  }

  function disableAll(url){
    batchProcess($("#disableButton"), url, '禁用','否');
  }  

  function batchProcess(btnObj, url, actinoName, value) {
    var $idsCheckedCheck = $("[name='ids']:checked");
    var $deleteButton = btnObj;// 删除按钮
    var ids = $idsCheckedCheck.serialize();
    if (confirm('您确定要'+actinoName+'吗？') == true) {       
        $.ajax({
            url : url,
            data : ids,
            dataType : "json",
            async : false,
            beforeSend : function(data) {
                $deleteButton.prop("disabled", true)
            },
            success : function(data) {
                $deleteButton.prop("disabled", false)
                if (data.status == "success") {
                    $idsCheckedCheck.parent().parent().find('#tariff_enabled').html(value);
                    if(actinoName == '禁用'){                        
                        $idsCheckedCheck.parent().parent().find('#tariff_operated').find('#edit-div').removeClass('am-hide');                       
                    }else if(actinoName =='启用'){
                        $idsCheckedCheck.parent().parent().find('#tariff_operated').find('#edit-div').addClass('am-hide');
                    }
                }
                sendMessage({
                    title : actinoName,
                    text : data.message,
                    type : "success",
                    timer : 2000,
                    showConfirmButton : false
                });                
            }
        });
    }
}
</script>
<%}%>